﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Robotics.Race
{
    public partial class CarPanel : UserControl
    {
        Car car = new Car(new NxtConfiguration());
        public Car Car { get { return car; } }
        
        public CarPanel()
        {
            InitializeComponent();
			car.UpdatedEvent += new Car.UpdatedHandler( car_UpdatedEvent );
        }

		public delegate void UpdateDelegate();

		void car_UpdatedEvent( Car car )
		{
			Invoke( new UpdateDelegate( UpdateStatus ) );
		}

		void UpdateStatus()
		{
			lbSpeed.Text = car.Speed.ToString();
            int value;
            int? test;

            test = car.Environment.LeftLight;

            lbLeftLightSensorHasValue.Visible = test == null;
            lbLeftLight.Visible = test != null;
            if (test != null)
            {
                lbLeftLight.Text = test + "";
                value = (int)((float)test * 2.55f);
            }
            else
                value = 0;
            panelLeftLightSensor.BackColor = Color.FromArgb(255 - value, value, 0);

            test = car.Environment.RightLight;
            
            lbRightLightSensorHasValue.Visible = test == null;
            lbRightLight.Visible = test != null;
            if (test != null)
            {
                lbRightLight.Text = test + "";
                value = (int)((float)test * 2.55f);
            }
            else
                value = 0;
            panelRightLightSensor.BackColor = Color.FromArgb(255 - value, value, 0);

            test = car.Environment.Distance;           
            lbSonarHasValue.Visible = test == null;
            lbDistance.Visible = test != null;
            if (test != null)
            {
                value = (int)test;
                lbDistance.Text = test + "";
            }
            else
                value = 0;
            panelSonar.BackColor = Color.FromArgb(value, value, value / 2);

            bool? touch = car.Environment.Touch;
            lbPressureSensorHasValue.Visible = touch == null;
            
            if (touch != null)
                value = (bool)touch ? 255 : 0;
            else
                value = 0;
            panelSonar.BackColor = Color.FromArgb(value, 0, 0);

            if (car.Nxt.Brick.IsConnected)
            {
                int level = car.Nxt.Brick.Comm.GetBatteryLevel();
                level = level / 100;
                lbBattery.Text = (level / 10) + "." + (level % 10) + "V";
            }
		}

        private void btCar_Click(object sender, EventArgs e)
        {
            frmCarConfiguration f = new frmCarConfiguration(car);
			f.ShowDialog();
			lbName.Text = car.Name != null && car.Name.Length > 0 ? car.Name : "[No name]";
            //car.Initialize();
        }

		private void button1_Click( object sender, EventArgs e )
		{
			frmDriverProfile f = new frmDriverProfile();
			f.LoadProfile( car.Driver );
			car.Driver = f.SelectProfile();
			lbDriver.Text = car.Driver == null ? "[No driver]" : car.Driver.FullName;
		}

        public void CloseThread()
        {
            car.TurnOffLights();
            car.Stop();
            car.CloseThread();
        }

        bool stopped = false;

        private void btStopStart_Click(object sender, EventArgs e)
        {
            stopped = !stopped;
            if (stopped)
            {
                car.Pause();
                btStopStart.Text = "Start";
            }
            else
            {
                car.Resume();
                btStopStart.Text = "Stop";
            }
        }
    }
}
